//
//  SN_Setting_ViewController.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/6/26.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_Setting_ViewController.h"
#import "SN_Security_Controller.h"
#import "SN_AboutSN_Controller.h"

#import "UMSocial.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"

@interface SN_Setting_ViewController () <UITableViewDelegate,UITableViewDataSource,UMSocialUIDelegate,UIAlertViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SN_Setting_ViewController

#pragma mark -Getter
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, VIEW_HEIGHT - SafeAreaTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = false;
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
        footerView.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
        _tableView.tableFooterView = footerView;
        _tableView.rowHeight = 44;
        
    }
    
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //配置QQ
    //ID1106187089
    [UMSocialQQHandler setQQWithAppId:@"1106187089" appKey:@"4BwR6I5NUkjFBPXu" url:@"http://admin.our100.net/d/szb.aspx"];
    
    //配置微信
    [UMSocialWechatHandler setWXAppId:@"wxb5a1ad234f1a4cc1" appSecret:@"e116b996063ef5db04fb15bd3faed380" url:@"http://admin.our100.net/d/szb.aspx"];
    
    self.navBar.navigationItem.title = @"设置";
    
    [self.view addSubview:self.tableView];
}


#pragma mark -UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }else if (section == 1) {
        return 2;
    }else {
        return 1;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.frame = CGRectMake(15, 14, 300, 16);
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
    
    if (indexPath.section == 0) {
        
        cell.textLabel.text = @"账号与安全";
    }
    
    if (indexPath.section == 2) {
        cell.textLabel.text = @"清除缓存";
    }
    
    if (indexPath.section == 1) {
        
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = @"推荐\"师云-少智版\"给好友";
                break;
            case 1:
                cell.textLabel.text = @"关于师云-少智版";
                break;
            default:
                break;
        }
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 1) {
        
        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 10)];
        v.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
        return v;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return section == 0 ? 0 : 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 && indexPath.row == 0) { //账号与安全
        
        SN_Security_Controller *vc = [[SN_Security_Controller alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if (indexPath.section == 1 && indexPath.row == 0) { //分享
        
        [UMSocialData defaultData].extConfig.qqData.title = @"师云少智版-在线学习好助手";
        [UMSocialData defaultData].extConfig.wechatSessionData.title = @"师云少智版-在线学习好助手";
        [UMSocialData defaultData].extConfig.wechatTimelineData.title = @"师云少智版-在线学习好助手";
        [UMSocialData defaultData].extConfig.qzoneData.title = @"师云少智版-在线学习好助手";
        
        

        [UMSocialSnsService presentSnsIconSheetView:self appKey:@"56eb6b22e0f55a9f9600000c" shareText:@"我在师云少智版看报解题，快来加入我吧！" shareImage:[UIImage imageNamed:@"APPicon170"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone, nil] delegate:self];

    }
    

    if (indexPath.section == 1 && indexPath.row == 1) { //关于师云-少智版
        
        SN_AboutSN_Controller *vc = [[SN_AboutSN_Controller alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if (indexPath.section == 2 && indexPath.row == 0) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否确认清除?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 1;
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag == 1) {
        
        if (buttonIndex == 1) {
            
            [[SDWebImageManager sharedManager].imageCache clearDiskOnCompletion:^{
               
                [self showHint:@"清除缓存完成"];
            }];
            
            [[DBUtil shareInstance] requestEditionAndGradeData];
        }
    }
}


@end
